/**
 * Created by zhangwj on 2017/1/11.
 */
function DemoAddController(Demo,Upload,API,$http,$uibModal,$scope,$state,$stateParams){
    var vm=this;
    vm.add=function(){
        vm.queryParams = _.clone($stateParams)
        Demo.add(vm.user).$promise.then(function(response){
            //处理add之后的返回结果
            if(response.success != null) {
                // 模拟窗口打开之后执行该函数
                var modalInstance = $uibModal.open({
                    templateUrl: 'myModalContent.html',
                    controller: 'ModalInstanceCtrl',
                    size: Demo
                });

                // 模拟窗口关闭后回传调用
                modalInstance.result.then(function () {
                    $state.go('userList', vm.queryParams)
                });
            } else {
                $scope.serverError = response.fail
                $("#error1").show();
            }
        })
    }

    vm.cancel = function() {
        $state.go('userList', vm.queryParams);
    }
    vm.invalid = function(){
        if($("#user_name").val()==""){
            $("#error1").hide();
        }
    }

    vm.upload = function(files) {
        if (files && files.length) {
            vm.files = files
            vm.form.$invalid = vm.uploading = true

            _.forEach(files, function(file) {
                vm.file=file;

                Upload.upload({ url: API + '/mobile/webUser/upload', file: file })
                    .progress(function(event) {
                        // TODO: maybe to implement a progress bar?
                        vm.progress = parseInt(100.0 * event.loaded / event.total)
                        //console && console.info('进度：' + vm.progress + '%')
                    })
                    .success(function(response) {
                        // vm.appRelease.fileUrl = response.data
                        vm.form.$invalid = vm.uploading = false
                    })
                    .error(function(response) {

                        vm.files = undefined
                        vm.form.$invalid = vm.uploading = false
                        debugger
                    })
            })
        }
    }


    //验证重复密码是否相同
    $scope.$watch('vm.user', function (newValue, oldValue) {
        if (newValue) {
            if (newValue.pass_word_reply) {
                if (!_.isEqual(newValue.pass_word_reply, newValue.pass_word)) {
                    vm.pwdInvalid = true
                    vm.form.pass_word_reply.$invalid = true
                    vm.form.$invalid = true
                } else {
                    vm.pwdInvalid = false
                    vm.form.pass_word_reply.$invalid = false
                }
            }
        }
    }, true)
}
